package Scene5;

import Picture.GameImage;

import java.awt.image.BufferedImage;

import java.util.ArrayList;

public class JumpAnimal {
    Ice1 ice1;
    Ice2 ice2;
    Ice3 ice3;
    Ice4 ice4;
    Ice5 ice5;
    Ice6 ice6;
    startIce si;

    int sign = 0;
    BufferedImage img;
    int w;
    int index = 0;
    ArrayList<BufferedImage> arrayListRight = new ArrayList<>();
    public JumpAnimal(){
        for (int i = 1; i < 7; i++) {
            arrayListRight.add(GameImage.get("/Picture/企鹅/企鹅1-"+i+".png"));
        }

    }
    public void changeImage(){
        if(index == 6){
            index  = 0;
        }
        img = arrayListRight.get(index);
        index++;

    }
    public int alive = 1;
    public int death = 0;
    //TODO 200
    int x  = 200;
    int y = 740;
    double g = 100;
    double speed = 0 ;
    double t = 0.25;
    double s;

    double gx = 100;
    double speedx = 0;
    double tx = 0.25;
    double sx;


    int groundY = 740;

    public void jump(){
        if(x>ice6.x&&x<ice6.x+ice6.w&&y==groundY){
            x+=20;
        }
        if(y>=groundY&&!(judegIce1()||judegIce2()||judegIce3()
                ||judegIce4()||judegIce5()||judegIce6()||judegSi())){
            alive = 0;
            speed = 0;
            y+=70;
        }
        else{


        if(sign==0) {

            double v0 = speed;
            s = v0 * t - 0.5 * g * t * t;
            double vt = v0 - g * t;
            speed = vt;
            y -= (int) s;
            if(y>=groundY) {
                y = groundY;
            }
        }
        else {
            double v0 = speed;
            s = v0 * t - 0.5 * g * t * t;
            double vt = v0 - g * t;
            speed = vt;
            y -= (int) s;


            double v0x = speedx;
            sx = v0x*tx ;
            double vtx = v0x ;
            speedx = vtx;
            x+=(int)sx;


            if(y>=groundY) {
                y = groundY;
                speedx = 0;
            }

            }
        }
    }

    private boolean judegSi() {

        if(this.x - si.x>si.w-10||this.x - si.x<-10){
            return false;
        }
        return true;
    }

    private boolean judegIce6() {
        if(this.x>ice6.x-120&&this.x<ice6.x+ice6.w){
            return true;
        }
        return false;
    }

    private boolean judegIce5() {
        if(this.x>ice5.x-120&&this.x<ice5.x+ice5.w){
            return true;
        }
        return false;
    }

    private boolean judegIce4() {
        if(this.x>ice4.x-120&&this.x<ice4.x+ice4.w){
            return true;
        }
        return false;
    }

    private boolean judegIce3() {
        if(this.x>ice3.x-120&&this.x<ice3.x+ice3.w){
            return true;
        }
        return false;
    }

    private boolean judegIce2() {
        if(this.x>ice2.x-120&&this.x<ice2.x+ice2.w){
            return true;
        }
        return false;
    }

    private boolean judegIce1() {
        if(this.x>ice1.x-140&&this.x<ice1.x+ice1.w+10){
            return true;
        }
        return false;
    }
    public void  reOpen(){
        x  = 200;
        y = 740;
        sign=0;

    }

}
